package com.cndym.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.codehaus.jackson.type.TypeReference;

import com.cndym.bean.Cooperation;
import com.cndym.exception.ErrCode;
import com.cndym.service.ICooperationService;
import com.cndym.serviceMap.ServiceBean;
import com.cndym.serviceMap.ServiceList;
import com.cndym.utils.JsonBinder;
import com.cndym.utils.Md5;
import com.cndym.utils.SpringUtils;
import com.cndym.utils.Utils;

/**
 * 浣滆�锛歠axc 鍒ゆ柇鎻愪氦鐨勫弬鏁版槸鍚︾鍚圡D5 楠岃瘉瑙勫垯(杩樺寘鍚竴浜涘叾浠栭獙璇�
 * 
 * 
 */
public class CheckMD5Filter implements Filter {
	
	private Logger logger = Logger.getLogger(getClass());
	
	Map<String, String> reMap = new HashMap<String, String>();
	
//	ICooperationService cooperationService = (ICooperationService) SpringUtils
//			.getBean("cooperationServiceImpl");
	
	PrintWriter out = null;
	
	private static String coopKey = "kkdYJjaMBSqEp6NLepGTxz2uyHn5FJxMCdGe7iAlJZWzSuvBt995uRARe2rbi8V7";

	public void destroy() {
	}

	public void doFilter(ServletRequest req, ServletResponse resp,
			FilterChain chain) throws ServletException, IOException {
		HttpServletRequest request = (HttpServletRequest) req;
		request.setCharacterEncoding("utf-8");
		HttpServletResponse response = (HttpServletResponse) resp;
		response.setCharacterEncoding("utf-8");
		response.reset();
		out = response.getWriter();
		String msg = Utils.formatStr(request.getParameter("msg"));
		Map<String, String> paraMap = new HashMap<String, String>();

		if (Utils.isNotEmpty(msg)) {
			try {
				paraMap = JsonBinder
						.buildNormalBinder()
						.getMapper()
						.readValue(msg,
								new TypeReference<Map<String, String>>() {
								});
			} catch (Exception e) {
				logger.error("", e);
				reMap.put("code", ErrCode.E3002);
				reMap.put("msg", ErrCode.codeToMsg(ErrCode.E3002));
				out.print(JsonBinder.buildNonDefaultBinder().toJson(reMap));
				out.close();
				return;

			}

			String md5 = paraMap.get("md5");
			
			List<String> tempList = new ArrayList<String>();
			for (Map.Entry<String, String> entry : paraMap.entrySet()) {
				tempList.add(entry.getKey());
			}
			java.util.Collections.sort(tempList);
			StringBuilder md5Str = new StringBuilder();
			for (String s : tempList) {
				if ("md5".equals(s) || "func".equals(s) || "ip".equals(s)
						|| "source".equals(s)) {
					continue;
				}
				md5Str.append(paraMap.get(s));
			}
			md5Str.append(coopKey);
			String currMd5 = Md5.Md5(md5Str.toString());
			if (!currMd5.equals(md5))
			{
				logger.error("currMd5: " + currMd5 + " paramMd5: " + md5);
				reMap.put("code", ErrCode.E0001);
				reMap.put("msg", ErrCode.codeToMsg(ErrCode.E0001));

				out.print(JsonBinder.buildNonDefaultBinder().toJson(reMap));
				out.close();
				return;
			}
		} else {
			reMap.put("code", ErrCode.E3005);
			reMap.put("msg", ErrCode.codeToMsg(ErrCode.E3005));
			out.print(JsonBinder.buildNonDefaultBinder().toJson(reMap));
			out.close();
			return;
		}
		chain.doFilter(request, response);
	}
	public void init(FilterConfig config) throws ServletException {
	}
}
